
package com.hexinfo.dmpro.script.config.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hexinfo.dmpro.common.base.PageResult;
import com.hexinfo.dmpro.common.model.ClusterSparingConf;
import com.hexinfo.dmpro.common.service.ClusterSparingConfService;
import com.hexinfo.dmpro.common.utils.CommonConstants;
import com.hexinfo.dmpro.script.config.model.ConfigPropertyParam;
import com.hexinfo.dmpro.script.config.model.ConfigPropertyPublishuser;
import com.hexinfo.dmpro.script.config.model.ConfigPropertyServer;
import com.hexinfo.dmpro.script.config.dao.ConfigPropertyServerMapper;
import com.hexinfo.dmpro.script.config.service.ConfigPropertyServerService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @Package com.hexinfo.dmpro.script.config.service.impl
 * @ClassName ConfigPropertyServerServiceImpl
 * @Description 服务器配置server表
 * @Author le.zhang
 * @Date 2024/3/4 13:13
 * @Version v1.0
 **/
@Service
public class ConfigPropertyServerServiceImpl extends ServiceImpl<ConfigPropertyServerMapper, ConfigPropertyServer> implements ConfigPropertyServerService {

    private final ClusterSparingConfService clusterSparingConfService;

    public ConfigPropertyServerServiceImpl(ClusterSparingConfService clusterSparingConfService) {
        this.clusterSparingConfService = clusterSparingConfService;
    }


    /**
     * @Method queryList
     * @Param page
     * @Param configPropertyServer
     * @Return com.hexinfo.dmpro.common.base.PageResult
     * @Description 分页查询
     * @Author le.zhang
     * @Date 2024/3/4 13:13
     * @Version V1.0
     */
    @Override
    public PageResult queryList(Page page, ConfigPropertyServer configPropertyServer) {
        LambdaQueryWrapper<ConfigPropertyServer> wrapper = beforeQuery(configPropertyServer);
        Page returnPage = this.baseMapper.selectPage(page, wrapper);
        List<ConfigPropertyServer> configPropertyServerList = returnPage.getRecords();
        afterQuery(returnPage.getRecords());
        return new PageResult(configPropertyServerList,returnPage.getTotal());
    }

    /**
     * @Method queryListNoPage
     * @Param configPropertyServer
     * @Return java.util.List<com.hexinfo.dmpro.script.config.model.ConfigPropertyServer>
     * @Description 不分页查询，可通过发布服务器标识查询，集群id查询
     * @Author le.zhang
     * @Date 2024/3/6 10:24
     * @Version V1.0
     */
    @Override
    public List<ConfigPropertyServer> queryListNoPage(ConfigPropertyServer configPropertyServer) {
        LambdaQueryWrapper<ConfigPropertyServer> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StrUtil.isNotBlank(configPropertyServer.getMark()), ConfigPropertyServer::getMark, configPropertyServer.getMark());
        wrapper.eq(StrUtil.isNotBlank(configPropertyServer.getClusterId()), ConfigPropertyServer::getClusterId, configPropertyServer.getClusterId());
        List<ConfigPropertyServer> configPropertyServers = this.baseMapper.selectList(wrapper);
        // 附加条件
        for (ConfigPropertyServer configPropertyServerInfo : configPropertyServers) {
            ClusterSparingConf byId = clusterSparingConfService.getById(configPropertyServerInfo.getClusterId());
            if (byId != null) {
                configPropertyServerInfo.setClusterName(byId.getClusterName());
                configPropertyServerInfo.setClusterWeight(byId.getClusterWeight());
            }

        }
        return configPropertyServers;
    }

    /**
     * @Method beforeQuery
     * @Param configPropertyServer
     * @Return com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<com.hexinfo.dmpro.script.config.model.ConfigPropertyServer>
     * @Description 分页前置查询，通过集群id精确查,区分标识模糊查
     * @Author le.zhang
     * @Date 2024/3/4 13:13
     * @Version V1.0
     */
    private LambdaQueryWrapper<ConfigPropertyServer> beforeQuery(ConfigPropertyServer configPropertyServer){
        LambdaQueryWrapper<ConfigPropertyServer> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotBlank(configPropertyServer.getClusterId())) {
            wrapper.eq(ConfigPropertyServer::getClusterId, configPropertyServer.getClusterId());
        }
        if(StrUtil.isNotBlank(configPropertyServer.getMark())){
            wrapper.like(ConfigPropertyServer::getMark,configPropertyServer.getMark());
        }
        return wrapper;
    }
    /**
     * @Method afterQuery
     * @Param configPropertyPublishuserList
     * @Return void
     * @Description 分页查询后置处理，将集群id转换为集群名称存入对象中
     * @Author le.zhang
     * @Date 2024/3/4 13:13
     * @Version V1.0
     */
    private void afterQuery(List<ConfigPropertyServer> configPropertyPublishuserList) {
        for (ConfigPropertyServer configPropertyServer : configPropertyPublishuserList) {
            ClusterSparingConf byId = clusterSparingConfService.getById(configPropertyServer.getClusterId());
            if (byId != null) {
                configPropertyServer.setClusterName(byId.getClusterName());
                configPropertyServer.setClusterWeight(byId.getClusterWeight());
            }

        }
    }

    /**
     * @Method findAll
     * @Param
     * @Return java.util.List<com.hexinfo.dmpro.script.config.model.ConfigPropertyServer>
     * @Description json配置文件到数据库后获取所有数据
     * @Author le.zhang
     * @Date 2024/3/4 13:14
     * @Version V1.0
     */
    public List<ConfigPropertyServer> findAll(){
        // TODO: 2024/4/1 留口：对于服务器权限从这里做限制 
        LambdaQueryWrapper<ConfigPropertyServer> wrapper = Wrappers.lambdaQuery();
        List<ConfigPropertyServer> serverList = this.baseMapper.selectList(wrapper);
        for (ConfigPropertyServer configPropertyServerInfo : serverList) {
            ClusterSparingConf byId = clusterSparingConfService.getById(configPropertyServerInfo.getClusterId());
            if (byId != null) {
                configPropertyServerInfo.setClusterName(byId.getClusterName());
                configPropertyServerInfo.setClusterWeight(byId.getClusterWeight());
            }
        }
        List<ConfigPropertyServer> configPropertyServerList = new ArrayList<>();
        for (ConfigPropertyServer c : serverList) {
            if (!("true".equals(c.getDisabled()))){
                configPropertyServerList.add(c);
            }
        }
        return configPropertyServerList;
    }
}
